package frame;


import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JTextField;

import bean.User;
import dao.UserDaoImpl;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JRadioButton;

public class UsersRegisterFrame implements ItemListener,ActionListener{

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;

    JRadioButton radioButton;
    JRadioButton radioButton_1;

    JButton button;
    JButton button_1;
    JButton button_2;
    ButtonGroup bg;

    String usex="";
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UsersRegisterFrame window = new UsersRegisterFrame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public UsersRegisterFrame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 607, 515);
        frame.setLocationRelativeTo(null); // Ĭ Ͼ     ʾ
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        frame.setVisible(true);

        JLabel lblL = new JLabel("\u7528\u6237\u6CE8\u518C\u754C\u9762");
        lblL.setFont(new Font("    ", Font.PLAIN, 30));
        lblL.setBounds(194, 43, 207, 52);
        frame.getContentPane().add(lblL);

        JLabel label_1 = new JLabel("\u7528\u6237\u540D\uFF1A");
        label_1.setFont(new Font("    ", Font.PLAIN, 20));
        label_1.setBounds(52, 132, 117, 31);
        frame.getContentPane().add(label_1);

        JLabel label_1_1 = new JLabel("\u5BC6\u7801\uFF1A");
        label_1_1.setFont(new Font("    ", Font.PLAIN, 20));
        label_1_1.setBounds(52, 219, 117, 31);
        frame.getContentPane().add(label_1_1);

        JLabel label_1_2 = new JLabel("\u786E\u8BA4\u5BC6\u7801\uFF1A");
        label_1_2.setFont(new Font("    ", Font.PLAIN, 20));
        label_1_2.setBounds(52, 263, 117, 31);
        frame.getContentPane().add(label_1_2);

        JLabel label_1_3 = new JLabel("\u6027\u522B\uFF1A");
        label_1_3.setFont(new Font("    ", Font.PLAIN, 20));
        label_1_3.setBounds(52, 175, 117, 31);
        frame.getContentPane().add(label_1_3);

        textField = new JTextField();
        textField.setBounds(134, 137, 86, 24);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(134, 224, 86, 24);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);

        textField_2 = new JTextField();
        textField_2.setBounds(159, 268, 86, 24);
        frame.getContentPane().add(textField_2);
        textField_2.setColumns(10);

        button = new JButton("\u63D0\u4EA4");
        button.setBounds(56, 364, 113, 27);
        frame.getContentPane().add(button);

        button_1 = new JButton("\u6E05\u9664");
        button_1.setBounds(225, 364, 113, 27);
        frame.getContentPane().add(button_1);

        button_2 = new JButton("\u8FD4\u56DE");
        button_2.setBounds(388, 364, 113, 27);
        frame.getContentPane().add(button_2);

        radioButton = new JRadioButton("\u7537");
        radioButton.setBounds(115, 179, 62, 27);
        frame.getContentPane().add(radioButton);

        radioButton_1 = new JRadioButton("\u5973");
        radioButton_1.setBounds(183, 179, 62, 27);
        frame.getContentPane().add(radioButton_1);

        //      Ůѡ   ѡһ
        bg=new ButtonGroup();
        bg.add(radioButton);
        bg.add(radioButton_1);

        radioButton.addItemListener(this);
        radioButton_1.addItemListener(this);
        button.addActionListener(this);
        button_1.addActionListener(this);
        button_2.addActionListener(this);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        // TODO Auto-generated method stub
        //    Ů Ա ѡ
        if(radioButton.isSelected()){
            usex=radioButton.getText();
        }else if(radioButton_1.isSelected()){
            usex=radioButton_1.getText();
        }
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String uname=textField.getText();
        String upwd=textField_1.getText();
        String reupwd=textField_2.getText();

        if(e.getSource()==button){
            UserDaoImpl udi=new UserDaoImpl();
            User user=new User();
            if(!upwd.equals(reupwd)) {
                JOptionPane.showMessageDialog(frame, "       벻һ £           ");
                textField_1.setText("");
                textField_2.setText("");
            }else {
                user.setUname(uname);
                user.setUpwd(upwd);
                user.setUsex(usex);
                int i=udi.AddUser(user);
                if(i!=0){
                    JOptionPane.showMessageDialog(frame, "ע  ɹ   ");
                    frame.setVisible(false);
                    UsersLoginFrame ulf=new UsersLoginFrame();
                }
            }


        }else if(e.getSource()==button_1){
            textField.setText("");
            textField_1.setText("");
            textField_2.setText("");
            bg.clearSelection();
        }else if(e.getSource()==button_2){
            UsersLoginFrame ul=new UsersLoginFrame();
            frame.setVisible(false);
        }

    }
}
